﻿using System;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Diagnostics;
using prototype123.models;
using prototype123.data_access;

namespace prototype123.controllers
{
    // MaintainCourseRegController handles operations involving adding/dropping/editing 
    // student course registration information.
    class MaintainCourseRegController
    {

        // AddCourseReg() shall add a termitem to the database and return the term item added
        // The parameters: <stu_id>,  <course_id>,  <textbook>,  <teachertype>,  <using_package> 
        // should typically be passed in from the UI.
        // Preconditions: We assume that all of the parameters have data filled in with valid data.
        // Postconditions: the database will be updated with a new Term_Item. Null is returned if the student has already passed the course.
        public TermItem AddCourseReg(int stu_id, string course_id, string textbook, string teachertype, bool using_package = false)
        {
            TermItemDAO tiDAO = new TermItemDAO(Connector.Instance.Connection);
            TermItem tm = new TermItem();
            StudentTermDAO sttermDAO = new StudentTermDAO(Connector.Instance.Connection);

            int term_id = sttermDAO.GetCurrentStudentTerm(stu_id);

            bool hasTaken = tiDAO.HasPassedCourse(stu_id, course_id);
            bool isTaking = tiDAO.IsTakingCourse(stu_id, course_id, term_id);

            if (hasTaken == true)
            {
                Debug.WriteLine(DateTime.Now + ": Student has already taken the course - MaintainCourseRegController.AddCourseReg");
                return null;
            }

            if (isTaking == true)
            {
                Debug.WriteLine(DateTime.Now + ": Student is currently taking the course - MaintainCourseRegController.AddCourseReg");
                return null;
            }

             // Throw a UserHasAlreadyTaken excpetion if this happens

            // If the user has already taken the course, but has dropped it, load that course, and clear the DROPPED flag in the existing TERM_ITEM Record
            bool hasTakenandDropped = tiDAO.HasDropped(stu_id, course_id, term_id);

            if (hasTakenandDropped == true)
            {
                tiDAO.ReAddCourse(stu_id, course_id, term_id);
                return tm;

            }

            TermItem tm_default = new TermItem(term_id, course_id, textbook, Convert.ToInt32(using_package), teachertype);

            tiDAO.AddCourse(SystemController.Instance.CurrentStudent, tm_default);
            Debug.WriteLine(DateTime.Now + "MaintainCourseRegController.AddCourseReg - " + course_id + " has been added.");
            return tm_default;           
        }

        // DropCourse() shall set the DROPPED flag in TERM_ITEM database table to 1 for a 
        // student's <stuid> specific course <courseid>. These parameters should be passed in from the UI.
        // Preconditions: We assume that all of the parameters have data filled in with valid data.
        // Postconditions: a row in TERM_ITEM's DROPPED flag will be turned to 1.
        public void DropCourse(int stuid, string courseid)
        {            
            StudentTermDAO sttermDAO = new StudentTermDAO(Connector.Instance.Connection);

            int termid = sttermDAO.GetCurrentStudentTerm(stuid);

            TermItemDAO tiDAO = new TermItemDAO(Connector.Instance.Connection);

            tiDAO.DropCourse(stuid, courseid, termid);
            Debug.WriteLine(DateTime.Now + "MaintainCourseRegController.DropCourse - " + courseid + " was dropped.");
        }

        // ReAddCourse() shall set the DROPPED flag in TERM_ITEM database table to 0 for a 
        // student's <stuid> specific course <courseid>. These parameters should be passed in from the UI.
        // Preconditions: We assume that all of the parameters have data filled in with valid data.
        // Postconditions: a row in TERM_ITEM's DROPPED flag will be turned to 0.
        public void ReAddCourse(int stuid, string courseid)
        {
            StudentTermDAO sttermDAO = new StudentTermDAO(Connector.Instance.Connection);

            int termid = sttermDAO.GetCurrentStudentTerm(stuid);
            TermItemDAO tiDAO = new TermItemDAO(Connector.Instance.Connection);

            tiDAO.ReAddCourse(stuid, courseid, termid);
            Debug.WriteLine(DateTime.Now + "MaintainCourseRegController.DropCourse - " + courseid + " was readded.");
        }



    }
}
